<template>
  <view>
    <view class="tip">根据国家药监局规定，购买处方药需要实名验证</view>
    <view class="container">
      <view class="title">基本信息</view>
      <view class="form-container">
        <view class="form-item">
          <view class="form-item-left">用药人姓名</view>
          <view class="form-item-right">
            <u-input v-model="patient.patientName" placeholder="请输入用药人真实姓名" />
          </view>
        </view>

        <view class="form-item">
          <view class="form-item-left">身份证号</view>
          <view class="form-item-right">
            <u-input v-model="patient.patientIdcard" placeholder="请输入身份证号码" />
          </view>
        </view>

        <view class="form-item">
          <view class="form-item-left">出生年月</view>
          <view class="form-item-right">
            <u-input v-model="patient.patientBirthday" placeholder="请输入出生年月" />
          </view>
        </view>

        <view class="form-item">
          <view class="form-item-left">性别</view>
          <view class="form-item-right">
            <radio-group style="display: flex;" @change="sexRadioChange">
              <view style="display: flex;justify-content: space-between;min-width: 100rpx">
                <radio :checked="patient.patientSex==1" class="gg-radio" :value="1" />
                <text>男</text>
              </view>
              <view style="display: flex;justify-content: space-between;min-width: 100rpx;margin-left: 80rpx;">
                <radio :checked="patient.patientSex==2" class="gg-radio" :value="2" />
                <text>女</text>
              </view>
            </radio-group>
          </view>
        </view>

        <view class="form-item">
          <view class="form-item-left">体重(kg)</view>
          <view class="form-item-right">
            <u-input v-model="patient.patientWeight" placeholder="请输入体重" />
          </view>
        </view>

        <view class="form-item">
          <view class="form-item-left">手机号码</view>
          <view class="form-item-right">
            <u-input v-model="patient.patientPhone" placeholder="手机号码" />
          </view>
        </view>

        <!--<view class="form-item">
          <view class="form-item-left">地址</view>
          <view class="form-item-right">
            <u-input class="input-sty" v-model="patient.patientAddress" placeholder="地址" />
          </view>
        </view>-->

        <view class="form-item no-border-bottom">
          <view class="form-item-left">关系</view>
          <view class="form-item-right">

            <view @click="changeRelationShopData(0)" class="tag" :class="{'tag-selected':patient.relationship==0}">本人</view>
            <view @click="changeRelationShopData(1)" class="tag" :class="{'tag-selected':patient.relationship==1}">家属</view>
            <view @click="changeRelationShopData(2)" class="tag" :class="{'tag-selected':patient.relationship==2}">亲戚</view>
            <view @click="changeRelationShopData(3)" class="tag" :class="{'tag-selected':patient.relationship==3}">朋友</view>

          </view>
        </view>

      </view>

      <view class="title">健康史</view>
      <view class="form-container">
        <view class="form-item form-item-second" @click="showModal('pmh')">
          <view class="form-item-left">过往病史</view>
          <view class="form-item-right item-right-spec">
            <u-icon class="icon" name="arrow-right"></u-icon>
            <text class="desc">{{patient.pmh?patient.pmh:'若无，可不填'}}</text>
          </view>
        </view>

        <view class="form-item form-item-second" @click="showModal('amh')">
          <view class="form-item-left">过敏史</view>
          <view class="form-item-right item-right-spec">
            <u-icon class="icon" name="arrow-right"></u-icon>
            <text class="desc">{{patient.amh?patient.amh:'若无，可不填'}}</text>
          </view>
        </view>

        <view class="form-item form-item-second" @click="showModal('fmh')">
          <view class="form-item-left">家族病史</view>
          <view class="form-item-right item-right-spec">
            <u-icon class="icon" name="arrow-right"></u-icon>
            <text class="desc">{{patient.fmh?patient.fmh:'若无，可不填'}}</text>
          </view>
        </view>

        <view class="form-item form-item-second" @click="showModal('liver')">
          <view class="form-item-left">肝功能</view>
          <view class="form-item-right item-right-spec">
            <u-icon class="icon" name="arrow-right"></u-icon>
            <text class="desc">{{patient.liver?patient.liver:'若正常，可不填'}}</text>
          </view>
        </view>

        <view class="form-item form-item-second" @click="showModal('renal')">
          <view class="form-item-left">肾功能</view>
          <view class="form-item-right item-right-spec">
            <u-icon class="icon" name="arrow-right"></u-icon>
            <text class="desc">{{patient.renal?patient.renal:'若正常，可不填'}}</text>
          </view>
        </view>

        <view class="form-item form-item-second" @click="showModal('nurse')">
          <view class="form-item-left">妊娠哺乳</view>
          <view class="form-item-right item-right-spec">
            <u-icon class="icon" name="arrow-right"></u-icon>
            <text class="desc">{{patient.nurse?patient.nurse:'若正常，可不填'}}</text>
          </view>
        </view>

      </view>

      <view class="form-container" style="margin-top: 16rpx" @click="changeDefaultData">
        <view class="form-item form-item-second no-border-bottom">
          <view class="form-item-left">默认用药人</view>
          <view class="form-item-right item-right-spec">
            <u-icon :style="'color: '+ (patient.is_default==1?'#09C15F':'#EDEDED')" name="checkmark-circle-fill"></u-icon>
          </view>
        </view>
      </view>

      <view style="height: 120rpx;background-color: #EDEDED"></view>
      <view class="btn-container">
        <button :disabled="submitBtnDisable" @click="submitBtn" class="content">{{patient.id?'修改':'新增用药人'}}</button>
      </view>

    </view>

    <u-modal v-model="modalShow" title="疾病描述" :zoom="false"
             :title-style="{color: '#333333','padding-top': '20rpx','font-weight': 'bold'}"
              @confirm="confirm()">
      <view class="slot-content">
        <view style="padding: 10rpx 30rpx 0 30rpx">
          <u-input :maxlength="1000" v-model="modalInput" :border-color="'#EDEDED'" type="textarea" border height="150" />
        </view>
      </view>
    </u-modal>

  </view>
</template>

<script>

  import {Prescription} from "../../model/prescription/Prescription";
  import {Patient} from "../../model/prescription/Patient";

  export default{
    data(){
      return {
        prescription: null,
        patient: null,
        modalShow: false,
        currentModal: '',
        modalInput: '',
        submitBtnDisable: false
      }
    },
    watch: {
      "patient.patientIdcard"(val){
        if(val.length==18){
          this.patient.setBirthdayFromIdCard()
        }
      }
    },
    onLoad(options) {
      this.prescription = new Prescription(this.$baseUrl)
      this.patient = new Patient()
      if(options.id){
        this.getUserDrugDetail(options.id)
      }
      let merchant = wx.getStorageSync('merchant')
      this.patient.patientAddress = merchant.address
    },
    methods:{

      //解除禁用提交按钮
      enableSubmitBtn() {
        this.submitBtnDisable = false
      },

      //禁用提交按钮
      disableSubmitBtn() {
        this.submitBtnDisable = true
      },

      sexRadioChange(e) {
        this.patient.patientSex = e.detail.value
      },

      showModal(type) {
        this.modalShow = true
        this.currentModal = type
        this.modalInput = this.patient.getHealthyInfo(type)
      },

      confirm() {
        this.patient.setHealthyInfo(this.currentModal,this.modalInput)
      },

      changeDefaultData() {
        this.patient.changeDefaultData()
      },

      changeRelationShopData(data) {
        this.patient.changeRelationShop(data)
      },

      async getUserDrugDetail(id) {
        const res = await this.$api.getUserDrugDetail({id},this.$baseUrl)
        this.patient.initData(res.data.data)
      },

      async submitBtn() {
        this.patient.setAge()
        this.disableSubmitBtn()
        wx.showLoading({
          title: '设置中'
        })
        const res = await this.prescription.setUserDrug(this.patient)
        let icon = ''
        if (res.data.level === "success") {
          icon = 'success'
          setTimeout(()=> {
            wx.navigateBack()
          },1200)
        } else {
          icon = 'none'
          this.enableSubmitBtn()
        }
        wx.hideLoading()
        this.$currency.showToast(res.data.message,icon)
      }

    },

  }
</script>

<style>
  /* 自定义 radio 样式 */
  /* radio 选中框样式 */
  .gg-radio .wx-radio-input{
    border-radius: 50%;
    width: 28rpx;
    height: 28rpx;
  }
  /* 选中后的 背景样式 （红色背景 无边框 可根据UI需求自己修改） */
  .gg-radio .wx-radio-input.wx-radio-input-checked{
    border: none;
    background: red;
  }
  /* 选中后的样式 （可根据设计稿需求自己修改） 对勾大小、背景颜色*/
  .gg-radio .wx-radio-input.wx-radio-input-checked::before{
    border-radius: 50%;
    width: 44rpx;
    height: 44rpx;
    line-height: 44rpx;
    text-align: center;
    font-size: 26rpx;
    color:#fff;
    background: #09C15F;
    border-color: #09C15F;
  }
</style>
<style lang="scss" scoped>

  .container{
    min-height: 100vh;
    background-color: #EDEDED;
    padding: 0 20rpx;
  }

  .tip{
    padding: 20rpx 0;
    background-color: #EDF0F7;
    font-size: 24rpx;
    display: flex;
    align-items: center;
    color: #333333;
    justify-content: center;
  }

  .title{
    padding: 20rpx 0 20rpx 20rpx;
    font-weight: bold;
  }

  .form-container{
    padding: 0 20rpx;
    background-color: #ffffff;
    border-radius: 20rpx;
    .no-border-bottom{
      border-bottom: none !important;
    }
    .form-item{
      display: flex;
      align-items: center;
      padding: 20rpx 0;
      border-bottom: 2rpx solid #EDEDED;
      min-height: 60rpx;
      .form-item-left{
        width: 170rpx;
        color: #333333;
        font-weight: bold;
        font-size: 28rpx;
      }
      .form-item-right{
        display: flex;
        .tag{
          display: inline-block;
          align-items: center;
          -moz-box-align: center;
          box-sizing: border-box;
          line-height: 1;
          border-radius: 100rpx;
          font-size: 22rpx;
          padding: 15rpx 25rpx;
          background-color: #EDEDED;
          color: grey;
          margin-right: 20rpx;
          margin-bottom: 8rpx;
        }
        .tag-selected{
          color: #ffffff;
          background-color: #09C15F;
        }
        .input-sty{
          width: calc(100vw - 250rpx);
        }
      }
      .item-right-spec{
        display: flex;
        align-items: center;
        flex-direction: row-reverse;
        width: calc(100% - 200rpx);
        .desc{
          font-size: 28rpx;
          color: grey;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          margin-right: 10rpx;
        }
        .icon{
          color: grey;
        }
      }
    }
    .form-item-second{
      justify-content: space-between;
    }

  }

  .btn-container{
    display:flex;
    flex-direction:row;
    justify-content: center;
    align-items: center;
    width: 710rpx;
    height: 120rpx;
    position: fixed;
    bottom: 0;
    /*left: 50%;*/
    color: #ffffff;
    background-color: #EDEDED;
    /*transform: translateX(-50%);*/
    .content{
      width: 75%;
      height: 90rpx;
      background-color: #09C15F;
      color: #ffffff;
      border-radius: 50rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }

  /deep/ .u-model__footer__button{
    height: 80rpx !important;
    line-height: 80rpx !important;
    color: #09C15F !important;
  }


</style>
